package com.cdshop.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import com.cdshop.comm.ApplicationException;
import com.cdshop.entity.User;
import com.cdshop.jdbc.PStatementCB2;
import com.cdshop.jdbc.ResultSetCB;

public final class UserDAO extends AbstractDAO<User> {

	protected static final UserDAO singleton = new UserDAO();

	public static UserDAO instance() {
		return singleton;
	}

	static final String Create_User_Query = "INSERT INTO User(username,nickname,password,email,sex,birthday,roles) VALUES(?,?,?,?,?,?,?)";
	static final String Validate_Username_Unique = "SELECT 1 FROM User WHERE username=?";
	static final String Validate_Email_Unique = "SELECT 1 FROM User WHERE email=?";

	static final String Login_User_Query = "SELECT * FROM User WHERE username=? AND password=?";

	static final PStatementCB2<User> User_PSCB = new PStatementCB2<User>() {
		@Override
		public void call(PreparedStatement pstmt, User entity)
				throws SQLException {
			int pos = 0;
			pstmt.setString(++pos, entity.getUsername());
			pstmt.setString(++pos, entity.getNickname());
			pstmt.setString(++pos, entity.getPassword());
			pstmt.setString(++pos, entity.getEmail());
			pstmt.setByte(++pos, entity.getSex());
			pstmt.setDate(++pos, new java.sql.Date(entity.getBirthday()
					.getTime()));
			pstmt.setString(++pos, entity.getRoles());
		}
	};
	static final ResultSetCB<User> User_RSCB = new ResultSetCB<User>() {

		@Override
		public User call(ResultSet rs, int count) throws SQLException {
			if (!rs.next())
				return null;
			User user = new User();
			user.setId(rs.getLong("id"));
			user.setUsername(rs.getString("username"));
			user.setNickname(rs.getString("nickname"));
			user.setPassword(rs.getString("password"));
			user.setEmail(rs.getString("email"));
			user.setSex(rs.getByte("sex"));
			user.setBirthday(rs.getDate("birthday"));
			user.setRoles(rs.getString("roles"));
			return user;
		}
	};

	@Override
	public User create(User entity) throws ApplicationException {
		try {
			long id = dbService.executeUpdate(Create_User_Query, User_PSCB,
					entity);
			if (id == -1) {
				return null;
			} else {
				// entity.setId(id); //Access not support JDBC 3.0
				return entity;
			}
		} catch (SQLException e) {
			throw new ApplicationException(e);
		}
	}

	@Override
	public User retrive(long id) throws ApplicationException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User update(User entity) throws ApplicationException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User delete(User entity) throws ApplicationException {
		// TODO Auto-generated method stub
		return null;
	}

	public User login(String username, String password)
			throws ApplicationException {
		Object[] values = { username, password };
		int[] types = { Types.VARCHAR, Types.VARCHAR };
		try {
			return dbService.executeQuery(Login_User_Query, values, types,
					User_RSCB);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public boolean validteUsernameUnique(String username)
			throws ApplicationException {
		Object[] params = { username };
		int[] types = { Types.VARCHAR };
		try {
			return !dbService.executeValidate(Validate_Username_Unique, params,
					types);
		} catch (SQLException e) {
			throw new ApplicationException(e);
		}
	}

	public boolean validteEmailUnique(String email) throws ApplicationException {
		Object[] params = { email };
		int[] types = { Types.VARCHAR };
		try {
			return !dbService.executeValidate(Validate_Email_Unique, params,
					types);
		} catch (SQLException e) {
			throw new ApplicationException(e);
		}
	}
}
